<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="身份证号" prop="userIdCard">
        <el-input style="width: 150px;"
          v-model="queryParams.userIdCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="driverName">
        <el-input style="width: 130px;"
          v-model="queryParams.driverName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="驾驶证号" prop="driverDrivingLicence">
        <el-input
          v-model="queryParams.driverDrivingLicence"
          placeholder="请输入从业人员驾驶证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="手机号" prop="phone" >
        <el-input style="width: 130px;"
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证状态" prop="isValid" >
        <el-select v-model="queryParams.isValid" placeholder="选择认证状态" clearable size="small"  style="width: 130px;">
          <el-option
            v-for="dict in dict_isValid"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:requirements:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:requirements:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:requirements:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:requirements:export']"
        >导出</el-button>
      </el-col>
    </el-row>-->

    <el-table v-loading="loading" :data="requirementsList" :max-height="tableHeight" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="姓名" align="center" prop="driverName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="userIdCard" />
<!--      <el-table-column label="驾驶证号" align="center" prop="driverDrivingLicence" />-->
      <el-table-column label="从业资格证号" align="center" prop="driverTransportationLicense" />
      <!--<el-table-column label="从业资格证初领日期" align="center" prop="driverTransportationInceptionDate" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.driverTransportationInceptionDate| dateString('YYYY-MM-DD') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="从业资格证有效期" align="center" prop="driverTransportationValidDate" width="180">
        <template slot-scope="scope">
          <span>{{scope.row.driverTransportationValidDate | dateString('YYYY-MM-DD') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="认证状态" align="center" prop="isValid" :formatter="isValidFormat" />
      <el-table-column label="提交认证时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="验证类型" align="center" prop="authenticationtype" :formatter="authenticationtypeFormat"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人员从业资格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">

        <el-col :span="12">
            <el-form-item label="姓名:" >{{form.driverName}}</el-form-item>
            <el-form-item label="身份证号:">{{form.userIdCard}}</el-form-item>
          </el-col>

        <el-col :span="12">
          <el-form-item label="手机号:" >{{form.phone}}</el-form-item>
          <el-form-item label="从业人员驾驶证号:" >{{form.driverDrivingLicence}}</el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="驾驶证有效期始:">{{form.driverDrivingStartDate}}</el-form-item>
          <el-form-item label="驾驶证有效时长:" >{{form.driverDrivingEndDate}}</el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="从业资格类别:">{{form.driverTransportationType}}</el-form-item>
          <el-form-item label="驾驶证准驾车型:">{{form.driverDrivingVehicleType}}</el-form-item>
        </el-col>

        <el-row>
          <el-col :span="12">
            <el-form-item label="资格证初领时间:" prop="driverTransportationInceptionDate">
              <el-date-picker clearable size="small" style="width: 150px" v-if="station_btn" disabled
                              v-model="form.driverTransportationInceptionDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择从业资格证初领时间">
              </el-date-picker>
              <el-date-picker clearable size="small" style="width: 150px" v-else
                              v-model="form.driverTransportationInceptionDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择从业资格证初领时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资格证有效时间:" prop="driverTransportationValidDate">
              <el-date-picker clearable size="small" style="width: 150px" v-if="station_btn" disabled
                              v-model="form.driverTransportationValidDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择从业资格证有效时间">
              </el-date-picker>
              <el-date-picker clearable size="small" style="width: 150px" v-else
                              v-model="form.driverTransportationValidDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择从业资格证有效时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
        <el-col :span="12">
          <el-form-item label="从业资格证号:"  prop="driverTransportationLicense">
            <el-input v-model="form.driverTransportationLicense" v-if="station_btn" disabled placeholder="" />
            <el-input v-model="form.driverTransportationLicense" v-else  placeholder="" />
          </el-form-item>
          <el-form-item label=""></el-form-item>
        </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="驾驶证主页照片:" v-if="form.driverDrivingFirstPageUrl != null && form.driverDrivingFirstPageUrl != ''">
              <el-image :src="form.driverDrivingFirstPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList">
                <div slot="error" class="image-slot">
                  暂无图片
                </div>
              </el-image>
              <span v-if="form.isValid == 0" style="color:red;">(识别证号：{{form.sbjszh}})</span>
            </el-form-item>
            <el-form-item label="驾驶证主页照片" v-else>
              暂无图片
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="驾驶证副页照片:" v-if="form.driverDrivingSecondPageUrl == undefined || form.driverDrivingSecondPageUrl == ''">
              暂无图片
            </el-form-item>
            <el-form-item label="驾驶证副页照片:" v-else>
              <el-image :src="form.driverDrivingSecondPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList">
                <div slot="error" class="image-slot">
                  暂无图片
                </div>
              </el-image>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="从业资格证书照片:" v-if="form.driverTransportationPageUrl != null && form.driverTransportationPageUrl != ''">
            <el-image :src="form.driverTransportationPageUrl" style="width: 100px; height: 100px" :preview-src-list="srcList" >
              <div slot="error" class="image-slot">
                暂无图片
              </div>
            </el-image>
          </el-form-item>
          <el-form-item label="从业资格证书照片" v-else>
            暂无图片
          </el-form-item>
        </el-row>


        <el-form-item label="是否验证通过:" prop="isValid" disabled>
          <el-radio-group v-model="form.isValid">
            <el-radio
              v-for="dict in dict_isValid"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="认证说明：" prop="validReason" >
          <el-input v-model="form.validReason" type="textarea" :rows="3" v-if="station_btn" disabled  placeholder="请输入内容" />
          <el-input v-model="form.validReason" type="textarea" :rows="3" v-else   placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRequirements, getRequirements, delRequirements, addRequirements, updateRequirements, exportRequirements } from "@/api/weight/requirements";


export default {
  name: "Requirements",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 人员从业资格表格数据
      requirementsList: [],
      // 认证状态
      dict_isValid: [],
      // 认证类型
      dict_authenticationtype: [],
      // 弹出层标题
      title: "",

      srcList:[],
      // 是否显示弹出层
      open: false,
      station_btn: false,
      tableHeight: window.innerHeight - 275,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        driverName: undefined,
        userIdCard: undefined,
        driverDrivingLicence: undefined,
        driverDrivingStartDate: undefined,
        driverDrivingEndDate: undefined,
        driverDrivingVehicleType: undefined,
        driverDrivingFirstPageUrl: undefined,
        driverDrivingSecondPageUrl: undefined,
        driverTransportationLicense: undefined,
        driverTransportationType: undefined,
        driverTransportationInceptionDate: undefined,
        driverTransportationValidDate: undefined,
        driverTransportationPageUrl: undefined,
        isValid: undefined,
        validReason: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        driverName: [
          { required: true, message: "人员姓名不能为空", trigger: "blur" }
        ],
        userIdCard: [
          { required: true, message: "人员身份证号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    // 认证状态
    this.getDicts("is_valid").then(response => {
      this.dict_isValid = response.data;
    });

    this.getDicts("authentication_type").then(response => {
      this.dict_authenticationtype = response.data;
    });

    const roles = this.$store.getters && this.$store.getters.roles;
    for (let rol of roles){
      if ('station'==rol || 'hs_station'==rol || 'xq_station'==rol || 'jy_station'==rol || 'yx_station'==rol){
        this.station_btn=true;
      }
    }


  },
  methods: {
    /** 查询人员从业资格列表 */
    getList() {
      this.loading = true;
      listRequirements(this.queryParams).then(response => {
        this.requirementsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        requirementId: undefined,
        driverName: undefined,
        userIdCard: undefined,
        driverDrivingLicence: undefined,
        driverDrivingStartDate: undefined,
        driverDrivingEndDate: undefined,
        driverDrivingVehicleType: undefined,
        driverDrivingFirstPageUrl: undefined,
        driverDrivingSecondPageUrl: undefined,
        driverTransportationLicense: undefined,
        driverTransportationType: undefined,
        driverTransportationInceptionDate: undefined,
        driverTransportationValidDate: undefined,
        driverTransportationPageUrl: undefined,
        isValid: undefined,
        validReason: undefined,
        status: "0",
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.requirementId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人员从业资格";
    },
    /** 修改按钮操作 */
    handleView(row) {
      this.reset();
      const requirementId = row.requirementId || this.ids
      getRequirements(requirementId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "人员认证详情";

        this.srcList.push(this.form.driverDrivingFirstPageUrl);
        this.srcList.push(this.form.driverDrivingSecondPageUrl);
        this.srcList.push(this.form.driverTransportationPageUrl);
      });
    },
     // 认证状态
    isValidFormat(row,column){
        return this.selectDictLabel(this.dict_isValid, row.isValid);
    },
    authenticationtypeFormat(row,column){
      return this.selectDictLabel(this.dict_authenticationtype, row.authenticationtype);
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.requirementId != undefined) {
            let _edit_form={
              requirementId: this.form.requirementId,
              driverTransportationInceptionDate: this.form.driverTransportationInceptionDate,
              driverTransportationValidDate: this.form.driverTransportationValidDate,
              driverTransportationLicense: this.form.driverTransportationLicense,
              isValid: this.form.isValid,
              validReason: this.form.validReason
            };
            updateRequirements(_edit_form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addRequirements(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const requirementIds = row.requirementId || this.ids;
      this.$confirm('是否确认删除人员从业资格编号为"' + requirementIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRequirements(requirementIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有人员从业资格数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRequirements(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
